package com.bwie.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.UserMapper;
import com.bwie.pojo.User;
import com.bwie.service.UserService;
import com.bwie.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * @author : LeiMingkun
 * @date : 2024-07-18 08:55
 * @description : UserServiceImpl
 **/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public Map getLogin(UserVo userVo) {
        Map<String, Object> map = new HashMap<>();
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(User::getUserName,userVo.getUserName());
        User user = userMapper.selectOne(wrapper);
        if(Objects.isNull(user)){
            map.put("status",false);
            map.put("msg","没有此账号");
            return map;
        }
        if(!user.getPassword().equals(userVo.getPassword())){
            map.put("status",false);
            map.put("msg","密码错误");
            return map;
        }

        return null;
    }

    @Override
    public Map getCode(UserVo userVo) {
        HashMap<String, Object> map = new HashMap<>();
        if(userVo.getUserName()!=null){
            ValueOperations<String, String> opsForValue = stringRedisTemplate.opsForValue();
            Boolean aBoolean = stringRedisTemplate.hasKey("user-" + userVo.getUserName());
            if(aBoolean){

            }else {
                String s = RandomUtil.randomNumbers(4);
                opsForValue.set("user-" + userVo.getUserName(),s,1, TimeUnit.MINUTES);
            }
        }
        map.put("status",true);
        map.put("msg","发送成功");
        return map;
    }
}